<template>
    <div id="distributor" class="distributor">
        <el-button v-if="addUnitId" @click="insertList" class="slot-btn-top" type="primary" size="small">添加</el-button>
        <el-table :data="dataList.slice((currentPage-1)*pagesize,currentPage*pagesize)" stripe border :height="tableHeight" :header-cell-style="getRowClass">
            <el-table-column fixed type="index" label="*" sortable min-width="40"></el-table-column>
            <el-table-column prop="name" label="企业名称" sortable min-width="250"></el-table-column>
            <el-table-column prop="phone" label="企业电话" sortable min-width="130"></el-table-column>
            <el-table-column prop="email" label="企业邮箱" sortable min-width="170"></el-table-column>
            <el-table-column prop="web" label="企业网址" sortable min-width="150"></el-table-column>
            <el-table-column prop="legalPerson" label="法人代表" sortable min-width="105"></el-table-column>
            <el-table-column prop="businessScope" label="经验范围" sortable min-width="300"></el-table-column>
            <el-table-column prop="registerDate" label="注册日期" sortable min-width="120"></el-table-column>
            <el-table-column fixed="right" label="操作" sortable align="center" width="90">
                <template slot-scope="scope">
                    <span v-if="unitId">
                        <el-button v-if="scope.row.approveResult != 1" @click="updateList(scope.row)" type="text" size="mini">编辑</el-button>
                        <el-button v-if="scope.row.approveResult != 1" @click="toExamineList(scope.row)" type="text" size="mini">审核</el-button>
                        <el-button v-if="scope.row.approveResult == 1" @click="toExamineList(scope.row)" type="text" size="mini">详情</el-button>
                    </span>
                    <span v-if="!unitId">
                        <el-button @click="toExamineList(scope.row)" type="text" size="mini">详情</el-button>
                    </span>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5,10, 15,20, 25,30,50, 100]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper" :total="dataList.length"></el-pagination>

        <!-- 详情框 -->
        <el-dialog :title="dialogTitle" width="80%" top="60px" :visible.sync="isDialog" :close-on-click-modal="false" style="padding-bottom: 15px;">
            <el-tabs v-model="activeName" @tab-click="handleClick" style="padding-bottom: 15px;">
                <el-tab-pane label="基本信息" name="1" :style="dialogHeight">
                    <h3 class="dialog-plate-title"><span>基本信息</span></h3>
                    <el-form :inline="true" class="demo-form-inline" label-position="right" label-width="150px" :model="dialogList">
                        <el-form-item label="企业名称"><el-input v-model="dialogList.name" :disabled="dialogTitle === '审核'"></el-input></el-form-item>
                        <el-form-item label="企业电话"><el-input v-model="dialogList.phone" :disabled="dialogTitle === '审核'"></el-input></el-form-item>
                        <el-form-item label="企业邮箱"><el-input v-model="dialogList.email" :disabled="dialogTitle === '审核'"></el-input></el-form-item>
                        <el-form-item label="企业网站"><el-input v-model="dialogList.web" :disabled="dialogTitle === '审核'"></el-input></el-form-item>
                        <el-form-item label="经销商管理人员"><el-select v-model="dialogList.jyManager" :disabled="dialogTitle === '审核'"><el-option v-for="item in jYManager"  :key="item.id" :value="item.id" :label="item.name"></el-option></el-select></el-form-item>
                    </el-form>
                    <el-form :inline="true" class="demo-form-inline" label-position="right" label-width="150px" :model="dialogList">
                        <el-form-item label="现企业经营地址">
                            <div style="width: 610px;">
                                <el-select :disabled="dialogTitle === '审核'" v-model="sheng" @change="choseSheng(sheng,'')" placeholder="省级地区"><el-option v-for="item in shengs" :key="item.value" :label="item.value" :value="item.value"></el-option></el-select>
                                <el-select :disabled="dialogTitle === '审核'" v-model="shi" @change="choseCity(shi,'')" placeholder="市级地区"><el-option v-for="item in shis" :key="item.value" :label="item.value" :value="item.value"></el-option></el-select>
                                <el-select :disabled="dialogTitle === '审核'" v-model="qu" @change="choseBlock(qu,'')" placeholder="区级地区"><el-option v-for="item in qus" :key="item.value" :label="item.value" :value="item.value"></el-option></el-select>
                                <el-input :disabled="dialogTitle === '审核'" v-model="dialogList.address" style="margin-top: 5px;" type="textarea" :rows="2" placeholder="请输入详细地址" ></el-input>
                            </div>
                        </el-form-item>
                    </el-form>
                    <el-form label-position="right" label-width="150px" :model="dialogList">
                        <el-form-item label="经营范围"><el-input v-model="dialogList.businessScope" :disabled="dialogTitle === '审核'" type="textarea" :row="3" style="width: 97%;"></el-input></el-form-item>
                        <el-form-item label="企业简介"><el-input v-model="dialogList.introduction" :disabled="dialogTitle === '审核'" type="textarea" :row="8" style="width: 97%;"></el-input></el-form-item>
                        <el-form-item label="备注说明"><el-input v-model="dialogList.remark" :disabled="dialogTitle === '审核'" type="textarea" :row="8" style="width: 97%;"></el-input></el-form-item>
                    </el-form>

                    <h3 class="dialog-plate-title"><span>工商信息</span></h3>
                    <el-form :inline="true" class="demo-form-inline" label-position="right" label-width="150px" :model="dialogList">
                        <el-form-item label="法人代表"><el-input v-model="dialogList.legalPerson" :disabled="dialogTitle === '审核'"></el-input></el-form-item>
                        <el-form-item label="注册资本（万元）"><el-input v-model="dialogList.registerCapital" :disabled="dialogTitle === '审核'"></el-input></el-form-item>
                        <el-form-item label="注册日期"><el-date-picker v-model="dialogList.registerDate" :disabled="dialogTitle === '审核'" type="date" placeholder="选择日期" style="width: 200px;"></el-date-picker></el-form-item>
                        <el-form-item label="统一社会信用代码"><el-input v-model="dialogList.creditCode" :disabled="dialogTitle === '审核'"></el-input></el-form-item>
                        <el-form-item label="登记机关"><el-input v-model="dialogList.registerOrgan" :disabled="dialogTitle === '审核'"></el-input></el-form-item>
                    </el-form>
                    <el-form :inline="true" class="demo-form-inline" label-position="right" label-width="150px" :model="dialogList">
                        <el-form-item label="工商注册地址">
                            <div style="width: 610px;">
                                <el-select :disabled="dialogTitle === '审核'" v-model="sheng1" @change="choseSheng(sheng1,'1')" placeholder="省级地区"><el-option v-for="item in shengs" :key="item.value" :label="item.value" :value="item.value"></el-option></el-select>
                                <el-select :disabled="dialogTitle === '审核'" v-model="shi1" @change="choseCity(shi1,'1')" placeholder="市级地区"><el-option v-for="item in shis" :key="item.value" :label="item.value" :value="item.value"></el-option></el-select>
                                <el-select :disabled="dialogTitle === '审核'" v-model="qu1" @change="choseBlock(qu1,'1')" placeholder="区级地区"><el-option v-for="item in qus" :key="item.value" :label="item.value" :value="item.value"></el-option></el-select>
                                <el-input :disabled="dialogTitle === '审核'" v-model="dialogList.registerAddress" style="margin-top: 5px;" type="textarea" :rows="2" placeholder="请输入详细地址" ></el-input>
                            </div>
                        </el-form-item>
                    </el-form>
                    <div v-if="dialogTitle !== '审核'" style="text-align: right;padding-right: 2%;"><el-button @click="submitList" type="primary">确定信息</el-button></div>

                    <h3 class="dialog-plate-title"><span>服务提供</span></h3>
                    <el-checkbox-group v-model="dialogServers" style="margin-top: 10px;">
                        <el-checkbox v-for="item in servers" :label="item.serviceName" :key="item.id" :disabled="dialogTitle === '审核'" >{{item.serviceName}}</el-checkbox>
                    </el-checkbox-group>
                    <div v-if="dialogTitle !== '审核'" style="text-align: right;padding-right: 2%;"><el-button @click="submitServers"  type="primary">提交服务</el-button></div>


                    <h3 class="dialog-plate-title" v-if="dialogTitle === '审核'" style="margin-top: 25px;"><span>审核信息</span></h3>
                    <el-form v-if="dialogTitle === '审核'" :inline="true" class="demo-form-inline" label-position="right" label-width="150px" :model="dialogList">
                        <el-form-item label="审核结果"><el-select v-model="dialogList.approveResult" :disabled="approvePass"><el-option value="1" label="审核通过"></el-option><el-option value="0" label="审核不通过"></el-option></el-select></el-form-item>
                        <el-form-item label="审核人"><el-input v-model="dialogList.approverName" disabled></el-input></el-form-item>
                        <el-form-item label="审核时间"><el-input v-model="dialogList.approveTime" disabled></el-input></el-form-item>
                    </el-form>
                    <el-form v-if="dialogTitle === '审核'" label-position="right" label-width="150px" :model="dialogList">
                        <el-form-item label="审核批注"><el-input v-model="dialogList.approveDesc" :disabled="approvePass" type="textarea" :row="2" style="width: 98%;"></el-input></el-form-item>
                    </el-form>

                    <div v-if="unitId">
                        <div v-if="dialogTitle === '审核'" style="text-align: right;padding-right: 2%;">
                            <el-button @click="submitList" :disabled="approvePass" type="primary">确定审核</el-button>
                        </div>
                    </div>


                </el-tab-pane>
                <el-tab-pane label="主要人员与登录用户" name="2" v-if="dialogTitle!=='添加'" :style="dialogHeight">
                    <h3 class="dialog-plate-title"><span>主要成员</span></h3>
                    <el-button v-if="unitId" @click="insertMemberList" class="slot-btn-top" type="primary" size="small" style="margin-top: 5px;">添加</el-button>
                    <el-table :data="dialogMemberData.slice((currentPage-1)*pagesize,currentPage*pagesize)" stripe border :height="dialogTabPaneTableHeight" :header-cell-style="getRowClass">
                        <el-table-column fixed type="index" label="*" sortable min-width="40"></el-table-column>
                        <el-table-column prop="name" label="人员名称" sortable min-width="120"></el-table-column>
                        <el-table-column prop="position" label="职位" sortable min-width="120"></el-table-column>
                        <el-table-column prop="phone" label="联系方式" sortable min-width="160"></el-table-column>
                        <el-table-column prop="createTime" label="填写时间" sortable min-width="170"></el-table-column>
                    </el-table>
                    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                                   :current-page="currentPage" :page-sizes="[5,10, 15,20, 25,30,50, 100]"
                                   :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper"
                                   :total="dialogMemberData.length"></el-pagination>

                    <h3 class="dialog-plate-title" style="margin-top: 30px;"><span>系统登录用户</span></h3>
                    <el-button v-if="unitId" @click="insertLoginUserList" class="slot-btn-top" type="primary" size="small" style="margin-top: 5px;">添加</el-button>
                    <el-table :data="dialogLoginUserData.slice((currentPage-1)*pagesize,currentPage*pagesize)" stripe border :height="dialogTabPaneTableHeight" :header-cell-style="getRowClass">
                        <el-table-column fixed type="index" label="*" sortable min-width="40"></el-table-column>
                        <el-table-column prop="loginName" label="登录名" sortable min-width="150"></el-table-column>
                        <el-table-column prop="permission" label="操作权限" sortable min-width="500"></el-table-column>
                    </el-table>
                    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5,10, 15,20, 25,30,50, 100]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper" :total="dialogLoginUserData.length"></el-pagination>
                </el-tab-pane>
                <el-tab-pane label="附件" name="3" v-if="dialogTitle!=='添加'" :style="dialogHeight">

                    <el-form :inline="true" class="demo-form-inline" label-position="right" label-width="110px" :model="dialogEnclosureList1" style="border-bottom: 1px solid #ececec;margin-bottom: 10px;">
                       <el-form-item :label="dialogEnclosureList1.name" style="margin-bottom: 65px;">
                            <el-upload class="avatar-uploader" ref="upload1" :data="dialogEnclosureSubData1" :auto-upload="false" :action="'/api/adscm/dealer/dealerAttachment/update?tokenId=' + tokenId" :limit="1" :on-success="handleAvatarSuccess" :on-error="handleAvatarError" :before-upload="beforeAvatarUpload">
                                <img v-if="dialogEnclosureList1.pathName" :src="dialogEnclosureList1.pathName" class="avatar"><i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>
                        </el-form-item>
                        <el-form-item label="*是否永久"><el-select @change='changeEnclosure("1")' v-model="dialogEnclosureList1.isForever"><el-option value="1" label="是"></el-option><el-option value="0" label="否"></el-option></el-select></el-form-item>
                        <el-form-item label="*到期时间" v-if="dialogEnclosureList1.isForever != '1'"><el-date-picker @change='changeEnclosure("1")' v-model="dialogEnclosureList1.date" type="date" placeholder="选择日期" style="width: 200px;"></el-date-picker></el-form-item>
                        <el-button v-if="unitId" @click='submitEnclosure("1")' type="primary">提交上传</el-button>
                    </el-form>
                    <el-form :inline="true" class="demo-form-inline" label-position="right" label-width="110px" :model="dialogEnclosureList2" style="border-bottom: 1px solid #ececec;margin-bottom: 10px;">
                        <el-form-item :label="dialogEnclosureList2.name" style="margin-bottom: 65px;">
                            <el-upload class="avatar-uploader" ref="upload2" :data="dialogEnclosureSubData2" :auto-upload="false" :action="'/api/adscm/dealer/dealerAttachment/update?tokenId=' + tokenId" :limit="1" :on-success="handleAvatarSuccess" :on-error="handleAvatarError" :before-upload="beforeAvatarUpload">
                                <img v-if="dialogEnclosureList2.pathName" :src="dialogEnclosureList2.pathName" class="avatar"><i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>
                        </el-form-item>
                        <el-form-item label="*是否永久"><el-select @change='changeEnclosure("2")' v-model="dialogEnclosureList2.isForever"><el-option value="1" label="是"></el-option><el-option value="0" label="否"></el-option></el-select></el-form-item>
                        <el-form-item label="*到期时间" v-if="dialogEnclosureList2.isForever != '1'"><el-date-picker @change='changeEnclosure("2")' v-model="dialogEnclosureList2.date" type="date" placeholder="选择日期" style="width: 200px;"></el-date-picker></el-form-item>
                        <el-button v-if="unitId" @click='submitEnclosure("2")' type="primary">提交上传</el-button>
                    </el-form>
                    <el-form :inline="true" class="demo-form-inline" label-position="right" label-width="110px" :model="dialogEnclosureList3" style="border-bottom: 1px solid #ececec;margin-bottom: 10px;">
                        <el-form-item :label="dialogEnclosureList3.name" style="margin-bottom: 65px;">
                            <el-upload class="avatar-uploader" ref="upload3" :data="dialogEnclosureSubData3" :auto-upload="false" :action="'/api/adscm/dealer/dealerAttachment/update?tokenId=' + tokenId" :limit="1" :on-success="handleAvatarSuccess" :on-error="handleAvatarError"  :before-upload="beforeAvatarUpload">
                                <img v-if="dialogEnclosureList3.pathName" :src="dialogEnclosureList3.pathName" class="avatar"><i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>
                        </el-form-item>
                        <el-form-item label="*是否永久"><el-select @change='changeEnclosure("3")' v-model="dialogEnclosureList3.isForever"><el-option value="1" label="是"></el-option><el-option value="0" label="否"></el-option></el-select></el-form-item>
                        <el-form-item label="*到期时间" v-if="dialogEnclosureList3.isForever != '1'"><el-date-picker @change='changeEnclosure("3")' v-model="dialogEnclosureList3.date" type="date" placeholder="选择日期" style="width: 200px;"></el-date-picker></el-form-item>
                        <el-button v-if="unitId" @click='submitEnclosure("3")' type="primary">提交上传</el-button>
                    </el-form>


                </el-tab-pane>
                <el-tab-pane label="填报记录" name="4" v-if="dialogTitle!=='添加'" :style="dialogHeight">
                    <el-table :data="dialogFillInData.slice((currentPage-1)*pagesize,currentPage*pagesize)" stripe border :height="dialogTabPaneFillInHeight" :header-cell-style="getRowClass">
                        <el-table-column fixed type="index" label="*" sortable min-width="40"></el-table-column>
                        <el-table-column prop="creatorName" label="填写人" sortable min-width="100"></el-table-column>
                        <el-table-column prop="time" label="填写时间" sortable min-width="170"></el-table-column>
                        <el-table-column prop="content" label="填写内容" sortable min-width="900"></el-table-column>
                    </el-table>
                    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5,10, 15,20, 25,30,50, 100]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper" :total="dialogFillInData.length"></el-pagination>
                </el-tab-pane>
            </el-tabs>
        </el-dialog>

        <!--主要成员-->
        <el-dialog title="添加" width="550px" center :visible.sync="isDialogMember" :close-on-click-modal="false" style="padding-bottom: 15px;">
            <el-form :inline="true" class="demo-form-inline" label-position="right" label-width="180px" :model="dialogMemberList">
                <el-form-item label="人员名称"><el-input v-model="dialogMemberList.name"></el-input></el-form-item>
                <el-form-item label="职位"><el-input v-model="dialogMemberList.position"></el-input></el-form-item>
                <el-form-item label="联系方式"><el-input v-model="dialogMemberList.phone"></el-input></el-form-item>
            </el-form>
            <div style="text-align: right;" slot="footer"><el-button @click="submitMemberList" type="primary">确 定</el-button></div>
        </el-dialog>

        <!--系统登录用户-->
        <el-dialog title="添加" width="450px" center :visible.sync="isDialogLoginUser" :close-on-click-modal="false" style="padding-bottom: 15px;">
            <el-form :inline="true" class="demo-form-inline" label-position="right" label-width="120px" :model="dialogLoginUserList">
                <el-form-item label="登录名"><el-input v-model="dialogLoginUserList.loginName"></el-input></el-form-item>
                <el-form-item label="操作权限"><el-input v-model="dialogLoginUserList.permission" type="textarea" :rows="8" style="width: 200px;"></el-input></el-form-item>
            </el-form>
            <div style="text-align: right;" slot="footer"><el-button @click="submitLoginUserList" type="primary">确 定</el-button></div>
        </el-dialog>


    </div>
</template>

<script>
    export default {
        name: "BasicInfo",
        data() {

            let roles = JSON.parse(sessionStorage.getItem("roles"));
            let unitId,addUnitId;

            console.log( roles )

            for (let i = 0;i<roles.length;i++) {
                if (roles[i].role_code==='ROLE_dealer_approver') {
                    unitId = true;
                }

                if (roles[i].role_code==='ROLE_dealer_admin') {
                    addUnitId = true;
                }
            }


            console.log(unitId,addUnitId )

            return {
                // 三级联动
                addressData: {},
                shengs: '',
                shis: [],
                qus: [],
                city: '',
                block: '',

                IP:'http://192.168.0.153:9999/',
                tokenId: sessionStorage.getItem("tokenId"),
                unitId: unitId,
                addUnitId:addUnitId,
                currentPage: 1,
                pagesize: 10,
                activeName: '1',
                tableHeight: addUnitId ? document.documentElement.clientHeight - 160 : document.documentElement.clientHeight - 130,
                dataList: [],
                jYManager: [],

                // 详情框
                isDialog: false,
                dialogTitle: '',
                dialogHeight: {
                    height: document.documentElement.clientHeight - 230+ 'px',
                    minHeight: document.documentElement.clientHeight - 230+ 'px',
                    overflowY: 'auto',
                },
                dialogList: {

                    address: '',
                    approveDesc: '',
                    approveResult: '',
                    approveTime: '',
                    approver: '',
                    approverName: '',
                    businessScope: '',
                    city: '',
                    creditCode: '',
                    dealerState: '',
                    email: '',
                    id: '',
                    introduction: '',
                    isManufacturer: '',
                    jyManager: '',
                    jyManagerName: '',
                    latitude: '',
                    legalPerson: '',
                    longitude: '',
                    name: '',
                    phone: '',
                    province: '',
                    registerAddress: '',
                    registerCapital: '',
                    registerCity: '',
                    registerDate: '',
                    registerOrgan: '',
                    registerProvince: '',
                    registerZone: '',
                    remark: '',
                    web: '',
                    zone: '',
                },
                sheng:'',
                shi:'',
                qu:'',
                sheng1:'',
                shi1:'',
                qu1:'',
                approvePass:'',

                //主要成员
                dialogMemberData: [],
                isDialogMember: false,
                dialogTabPaneTableHeight: unitId ?  (document.documentElement.clientHeight - 440) / 2 :  (document.documentElement.clientHeight - 380) / 2,
                dialogMemberList: {
                   did:"",
                   name:"",
                   position:"",
                   phone:"",
                   creator:"",
                   createTime:"",
                },
                //系统登录用户
                dialogLoginUserData: [],
                isDialogLoginUser: false,
                dialogLoginUserList: {
                    did:'',
                    loginName:'',
                    permission:'',
                    creator:'',
                    createTime:'',
                },


                //服务提供
                servers:[],
                dialogServers:[],

                //附件
                enclosureImageUrl: '',
                enclosureIdx:'',
                headers: {'deviceCode': 'A95ZEF1-47B5-AC90BF3'},
                dialogEnclosureSubData1:{dealerAttachment:''},
                dialogEnclosureSubData2:{dealerAttachment:''},
                dialogEnclosureSubData3:{dealerAttachment:''},
                dialogEnclosureList1:{createTime: "", creator: "", date: "", did: "", id: "", isForever: "", name: "", path: "",pathName: ""},
                dialogEnclosureList2:{createTime: "", creator: "", date: "", did: "", id: "", isForever: "", name: "", path: "",pathName: ""},
                dialogEnclosureList3:{createTime: "", creator: "", date: "", did: "", id: "", isForever: "", name: "", path: "",pathName: ""},

                //填报记录
                dialogFillInData:[],
                dialogTabPaneFillInHeight: document.documentElement.clientHeight - 270,

            }
        },
        created() {
            this.getData();
            this.getAllCityData();
            this.getJYManagerData();
            this.getServer();
        },
        methods: {

            getJYManagerData() {
                let _this = this;
                this.$http({
                    url: '/api/adscm/system/user/queryUsersByRoleId/7?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                }).then(function (res) {
                    console.log(res.data)
                    if (res.data.statusCode == 200) {
                        _this.jYManager = res.data.content;
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })

            },
            getData() {
                let _this = this;
                let loading = this.$loading({
                    target: document.querySelector(".wrapper > .content-box"),
                    lock: true,
                    text: '正在努力加载中...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.8)'
                });
                this.$http({
                    url: '/api/adscm/dealer/dealer/selectAll?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                }).then(function (res) {

                    console.log(res.data)

                    if (res.data.statusCode == 200) {
                        _this.dataList = res.data.content;
                        loading.close();
                    } else {
                        _this.$message.error(res.data.desc);
                        loading.close();
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                    loading.close();
                })
            },
            getAllCityData(){
                let _this = this;
                this.$http.get('.././../static/json/map.json').then(function (response) {
                    if (response.status == 200) {
                        let data = response.data;
                        _this.addressData = response.data;
                        _this.shengs = [];
                        _this.city = [];
                        _this.block = [];
                        // 省市区数据分类
                        for (let item in data) {
                            if (item.match(/0000$/)) {//省
                                _this.shengs.push({id: item, value: data[item], children: []})
                            } else if (item.match(/00$/)) {//市
                                _this.city.push({id: item, value: data[item], children: []})
                            } else {//区
                                _this.block.push({id: item, value: data[item]});
                            }
                        }
                        // 分类市级
                        for (let index in _this.shengs) {
                            for (let index1 in _this.city) {
                                if (_this.shengs[index].id.slice(0, 2) === _this.city[index1].id.slice(0, 2)) {
                                    _this.shengs[index].children.push(_this.city[index1]);
                                }
                            }
                        }
                        // 分类区级
                        for (let item1 in _this.city) {
                            for (let item2 in _this.block) {
                                if (_this.block[item2].id.slice(0, 4) === _this.city[item1].id.slice(0, 4)) {
                                    _this.city[item1].children.push(_this.block[item2]);
                                }
                            }
                        }
                    } else {console.log(response.status);}
                }).catch(function (error) {console.log(typeof +error)})
            },
            //基本信息
            insertList() {
                this.isDialog = true;
                this.activeName = "1";
                this.dialogTitle = '添加';
                this.dialogList= {
                    address: '',
                    approveDesc: '',
                    approveResult: '',
                    approveTime: '',
                    approver: '',
                    approverName: '',
                    businessScope: '',
                    city: '',
                    creditCode: '',
                    dealerState: '',
                    email: '',
                    id: '',
                    introduction: '',
                    isManufacturer: '',
                    jyManager: '',
                    jyManagerName: '',
                    latitude: '',
                    legalPerson: '',
                    longitude: '',
                    name: '',
                    phone: '',
                    province: '',
                    registerAddress: '',
                    registerCapital: '',
                    registerCity: '',
                    registerDate: '',
                    registerOrgan: '',
                    registerProvince: '',
                    registerZone: '',
                    remark: '',
                    web: '',
                    zone: '',
                };
                this.sheng= '';
                this.shi= '';
                this.qu= '';
                this.sheng1= '';
                this.shi1= '';
                this.qu1= '';
                this.dialogServers=[];
            },
            updateList(row) {
                this.isDialog = true;
                this.activeName = "1";
                this.dialogTitle = '编辑';
                this.dialogList = row;
                this.sheng= row.province;
                this.shi= row.city;
                this.qu= row.zone;
                this.sheng1= row.registerProvince;
                this.shi1= row.registerCity;
                this.qu1= row.registerZone;
                this.dialogList.jyManager = parseInt(row.jyManager);
                this.setServer();
            },
            toExamineList( row ){
                this.isDialog = true;
                this.activeName = "1";
                this.dialogTitle = '审核';
                this.dialogList = row;
                this.sheng= row.province;
                this.shi= row.city;
                this.qu= row.zone;
                this.sheng1= row.registerProvince;
                this.shi1= row.registerCity;
                this.qu1= row.registerZone;
                this.dialogList.jyManager = parseInt(row.jyManager);
                this.approvePass = row.approveResult == 1 ? true : false;
                this.setServer();
            },
            submitList() {
                let _this = this;
                let url;
                if (this.dialogTitle === '添加') {
                    url = '/api/adscm/dealer/dealer/insert?tokenId=' + _this.tokenId;
                }else if (this.dialogTitle === '编辑') {
                    url = '/api/adscm/dealer/dealer/update?tokenId=' + _this.tokenId;
                } else if (this.dialogTitle === '审核') {
                    url = '/api/adscm/dealer/dealer/update?tokenId=' + _this.tokenId;
                }

                _this.dialogList.province = _this.sheng;
                _this.dialogList.city = _this.shi;
                _this.dialogList.zone = _this.qu;
                _this.dialogList.registerProvince = _this.sheng1;
                _this.dialogList.registerCity =_this.shi1;
                _this.dialogList.registerZone = _this.qu1;
                _this.dialogList.registerDate = _this.timestampToTime( _this.dialogList.registerDate).substr(0,10);

                console.log(_this.dialogList)

                this.$http({
                    url: url,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                    data: _this.dialogList
                }).then(function (res) {
                    console.log(res.data)
                    if (res.data.statusCode == 200) {
                        _this.isDialog = false;
                        _this.getData();
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })

            },


            //服务提供
            getServer(){
                let _this = this;
                this.$http({
                    url: '/api/adscm/serviceProvider/jyServiceType/selectAll?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                }).then(function (res) {
                    console.log(res.data)
                    if (res.data.statusCode == 200) {
                        _this.servers = res.data.content;
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })
            },
            setServer(){
                let _this = this;
                this.$http({
                    url: '/api/adscm/dealer/dealerServiceRelation/selectByDid/'+_this.dialogList.id+'?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                }).then(function (res) {
                    console.log(res.data)
                    _this.dialogServers = [];
                    if (res.data.statusCode == 200) {
                        for (let i = 0;i<res.data.content.length;i++) {
                            let item = res.data.content[i];
                            _this.dialogServers.push(item.serviceIdName);
                        }
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })
            },
            submitServers(){
                let _this = this;
                let data =[];
                for (let i = 0;i<_this.servers.length;i++) {
                    for (let j = 0;j<_this.dialogServers.length;j++) {
                        if (_this.servers[i].serviceName == _this.dialogServers[j] ) {
                            data.push({
                                did: _this.dialogList.id,
                                id: "",
                                serviceId:  _this.servers[i].id,
                                serviceIdName: _this.dialogServers[j]
                            })
                        }
                    }
                }

                this.$http({
                    url: '/api/adscm/dealer/dealerServiceRelation/insert?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                    data:data,
                }).then(function (res) {
                    console.log(res.data)
                    if (res.data.statusCode == 200) {
                        _this.isDialog = false;
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })
            },




            //主要成员
            getMemberData() {
                let _this = this;
                this.$http({
                    url: '/api/adscm/dealer/dealerMembers/selectByDid/' + _this.dialogList.id + '?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                }).then(function (res) {
                    console.log(res.data)
                    if (res.data.statusCode == 200) {
                        _this.dialogMemberData = res.data.content;
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })
            },
            insertMemberList() {
                this.isDialogMember = true;
                this.dialogMemberList= {
                    did:"",
                    name:"",
                    position:"",
                    phone:"",
                    creator:"",
                    createTime:"",
                };
            },
            submitMemberList() {
                let _this = this;
                _this.dialogMemberList.did = _this.dialogList.id;

                console.log(_this.dialogMemberList)
                this.$http({
                    url: '/api/adscm/dealer/dealerMembers/insert?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                    data: _this.dialogMemberList
                }).then(function (res) {

                    console.log(res.data)

                    if (res.data.statusCode == 200) {
                        _this.isDialogMember = false;
                        _this.getMemberData()
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })

            },

            //系统登录用户
            getLoginUserData() {
                let _this = this;
                this.$http({
                    url: '/api/adscm/dealer/dealerSystemLoginUser/selectByDid/' + _this.dialogList.id + '?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                }).then(function (res) {
                    console.log(res.data)
                    if (res.data.statusCode == 200) {
                        _this.dialogLoginUserData = res.data.content;
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })
            },
            insertLoginUserList() {
                this.isDialogLoginUser = true;
                this.dialogLoginUserList= {
                    did:'',
                    loginName:'',
                    permission:'',
                    creator:'',
                    createTime:'',
                };
            },
            submitLoginUserList() {
                let _this = this;
                _this.dialogLoginUserList.did = _this.dialogList.id;

                console.log(_this.dialogLoginUserList)

                this.$http({
                    url: '/api/adscm/dealer/dealerSystemLoginUser/insert?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                    data: _this.dialogLoginUserList
                }).then(function (res) {
                    console.log(res.data)
                    if (res.data.statusCode == 200) {
                        _this.isDialogLoginUser = false;
                        _this.getLoginUserData();
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })

            },


            //附件
            getEnclosureData() {
                let _this = this;
                this.$http({
                    url: '/api/adscm/dealer/dealerAttachment/selectByDid/' + _this.dialogList.id + '?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                }).then(function (res) {
                    console.log(res.data)
                    if (res.data.statusCode == 200) {
                        let item1 =  res.data.content[0],item2 =  res.data.content[1],item3 =  res.data.content[2];
                        _this.dialogEnclosureList1 = item1;
                        _this.dialogEnclosureList2 = item2;
                        _this.dialogEnclosureList3 = item3;
                        _this.dialogEnclosureList1.pathName =  item1.pathName ? (_this.IP + item1.pathName) : item1.pathName;
                        _this.dialogEnclosureList2.pathName =  item2.pathName ? (_this.IP + item2.pathName) : item2.pathName;
                        _this.dialogEnclosureList3.pathName =  item3.pathName ? (_this.IP + item3.pathName) : item3.pathName;
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })
            },

            changeEnclosure(index) {
                this['dialogEnclosureList'+index].date = this.timestampToTime(this['dialogEnclosureList'+index].date);
                this['dialogEnclosureSubData'+index] = {dealerAttachment: JSON.stringify( this['dialogEnclosureList'+index])};
            },
            submitEnclosure(index) {
                this.enclosureIdx = index;
                let el = this.$refs['upload'+index];
                this['dialogEnclosureList'+index].date = this.timestampToTime(this['dialogEnclosureList'+index].date);
                this['dialogEnclosureSubData'+index] = {dealerAttachment: JSON.stringify(this['dialogEnclosureList'+index])};
                console.log( this['dialogEnclosureSubData'+index] )
                console.log( el )
                el.submit();
            },
            beforeAvatarUpload(file) {

                console.log( file )

                const isJPG = file.type.substr(0,5) === 'image';
                const isLt2M = file.size / 1024 / 1024 < 10;
                if (!isJPG) {
                    this.$message.error('只能上传图片!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 10MB!');
                }
                return isJPG && isLt2M;
            },
            handleAvatarSuccess(res, file,fileList) {
                console.log(res,file,fileList )
                let idx = this.enclosureIdx;
                this['dialogEnclosureList'+idx].pathName = URL.createObjectURL(file.raw);
            },
            handleAvatarError(res, file,fileList) {
                console.log( res, file,fileList )
                this.$message.error('上传失败！');
            },



            // 填报记录
            getFullInData() {
                let _this = this;
                this.$http({
                    url: '/api/adscm/dealer/dealerInfo/selectByDid/' + _this.dialogList.id + '?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                }).then(function (res) {
                    console.log(res.data)
                    if (res.data.statusCode == 200) {
                        _this.dialogFillInData = res.data.content;
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })
            },





            // 加载china地点数据，三级
            choseSheng: function (e,index) {
                for (let index2 in this.shengs) {
                    if (e === this.shengs[index2].value) {
                        this.shis = this.shengs[index2].children;
                        this['shi'+index] = this.shengs[index2].children[0].value;
                        this.qus = this.shengs[index2].children[0].children;
                        this['qu'+index] = this.shengs[index2].children[0].children[0].value;
                        this.E = this.qus[0].value;
                    }
                }
            },
            choseCity: function (e,index) {
                for (let index3 in this.city) {
                    if (e === this.city[index3].value) {
                        this.qus = this.city[index3].children;
                        this['qu'+index] = this.city[index3].children[0].value;
                        this.E = this.qus[0].value;
                    }
                }

            },
            choseBlock: function (e,index) {
                this.E = e;
            },


            handleSizeChange: function (size) {
                this.pagesize = size;
            },
            handleCurrentChange: function (currentPage) {
                this.currentPage = currentPage;
            },
            handleClick(tab, event) {
                this.pagesize=10;
                this.currentPage=1;
                if (tab.name === '2') {
                    this.getMemberData();
                    this.getLoginUserData();
                } else if (tab.name === '3') {
                    this.getEnclosureData();
                } else if ( tab.name === '4' ) {
                    this.getFullInData();
                }
            },
            formatTimestampToTime(row, column, cellValue, index) {
                return cellValue ? this.timestampToTime(cellValue) : '';
            },
            timestampToTime(timestamp) {
                if (timestamp) {
                    let date = new Date(timestamp);
                    let Y = date.getFullYear() + '-';
                    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
                    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + " ";
                    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ":";
                    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ":";
                    let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
                    return timestamp ? Y + M + D + h + m + s : '';
                } else {
                    return '';
                }
            },

        }
    }
</script>

<style scoped></style>
